#include "main.h"
//套接字变量
int S,C[3],n=0;                 
struct login C_login[3]={0};
struct Plink*P=NULL;
int main(int argc, const char *argv[])
{
	//搭建客户端
	//1、创建套接字
	int C=-1;
	if((C=socket(AF_INET,SOCK_STREAM,0))==-1){perror("创建套接字失败");return -1;}
	//2、请求连接
	struct sockaddr_in addr={0};
	addr.sin_family=AF_INET;
	addr.sin_port=htons(12345);
	addr.sin_addr.s_addr=inet_addr("0.0.0.0");
	if(connect(C,(struct sockaddr*)&addr,sizeof(addr))==-1){perror("绑定失败");return -1;}
	struct login C_login={0};
	int ret;
	while(1)
	{
		printf("请输入账号:");
		scanf("%20s",C_login.name);
		while(getchar()!=10);
		printf("请输入密码:");
		scanf("%20s",C_login.password);
		while(getchar()!=10);
		//发送用户数据
		if(write(C,&C_login,sizeof(C_login))==-1){perror("发送至服务器失败");break;}
		//读取服务器反馈
		if(read(C,&ret,4)==0){printf("服务器关闭\n");break;}
		if(ret==0)
		{
			printf("验证正确\n");
			break;
		}
		else
			printf("验证失败\n");
	}
	//接收服务器端消息线程
	pthread_t pth;
	pthread_create(&pth,NULL,create_client,&C);
	struct pack txt={0};
	while(1)
	{
		printf("请输入收件人：");
		scanf("%d",&txt.client);
		while(getchar()!=10);
		printf("请输入消息文本：");
		scanf("%s",txt.date);
		while(getchar()!=10);
		txt.packsize=strlen(txt.date);
		write(C,&txt,txt.packsize+8);
	}
	//关闭文件
	pthread_join(pth,NULL);
	printf("关闭文件\n");
	close(C);
	return 0;
}
